﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Html5Music.Data
{
    public class PlayListData : DataBaseForApi<Dictionary<Guid, Model.PlayList>>
    {
        public PlayListData()
            : base(Config.Key_PlayList)
        {
            base.Load();
        }

        public Model.PlayList GetByID(Guid id)
        {
            if (this.Data.ContainsKey(id))
            {
                return this.Data[id];
            }

            return null;
        }

        public List<Model.PlayList> GetList()
        {
            this.Load();
            List<Model.PlayList> list = new List<Model.PlayList>();
            foreach (var item in this.Data)
            {
                list.Add(item.Value);
            }

            return list;
        }
        public Model.PlayList GetByKey(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                return null;
            }
            this.Load();
            foreach (var item in base.Data)
            {
                if (key.ToLower().Contains(item.Value.Key.ToLower()))
                {
                    return item.Value;
                }
            }

            return null;
        }

        public List<Model.PlayList> GetListByUserID(Guid userID)
        {
            List<Model.PlayList> list = new List<Model.PlayList>();
            foreach (var item in base.Data)
            {
                if (item.Value.UserID == userID)
                {
                    list.Add(item.Value);
                }
            }

            return list;
        }

        public bool KeyExist(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                return true;
            }
            this.Load();
            foreach (var item in base.Data)
            {
                if (key.ToLower().Contains(item.Value.Key.ToLower()))
                {
                    return true;
                }
            }

            return false;
        }

        public bool Add(Model.PlayList data)
        {
            if (KeyExist(data.Key))
            {
                return false;
            }
            data.ID = Guid.NewGuid();

            this.Data.Add(data.ID, data);
            this.Save();

            return true;
        }

        public bool Update(Model.PlayList data)
        {
            if (!this.Data.ContainsKey(data.ID))
            {
                return false;
            }
            this.Data[data.ID] = data;
            this.Save();

            return true;
        }

        public bool AddOrUpdate(Model.PlayList data)
        {
            if (!this.Data.ContainsKey(data.ID))
            {
                data.ID = this.Data[data.ID].ID;
                this.Data[data.ID] = data;
            }
            else
            {
                if (KeyExist(data.Key))
                {
                    return false;
                }
                data.ID = Guid.NewGuid();
                this.Data.Add(data.ID, data);
            }
            this.Save();

            return true;
        }

        public bool Delete(Guid id)
        {
            this.Load();
            if (!this.Data.ContainsKey(id))
            {
                return false;
            }
            this.Data.Remove(id);
            this.Save();

            return true;
        }
    }
}
